package com.whzykj.common;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.ehcache.CacheKit;
import com.whzykj.model.Dictionary;

public class DictCache {
	
	public static final DictCache cache=new DictCache();
	/*******
	 * 系统缓存所有字典明细说明
	 * 09.10
	 * @author yuandj
	 */
	public void loadCache() {
		List<Dictionary> dataDicList=Dictionary.me.loadDicCache();
		CacheKit.put("dictCache", "dic", dataDicList);		
	}
	
	/*******
	 * 查找内存中值对应的code
	 * 10.10
	 * @param dicType
	 * @param dicName
	 * @return
	 */
	public String getDicByText(String dicCode) {
		String str = "";
		List<Dictionary> list =CacheKit.get("dictCache", "dic");
		for (Dictionary dic : list) {
			String diccode = dic.getStr("dict_code");
			String dicname = dic.getStr("dict_name");
			if (dicCode.equals(diccode)) {
				str = dicname;
				break;
			}
		}
		return str;
	}
	
	/*******
	 * 查找内存中值对应的code
	 * 10.10
	 * @param dicType
	 * @param dicName
	 * @return
	 */
	public List<Dictionary> getDicByDictype(String dicType) {
		List<Dictionary> list =CacheKit.get("dictCache", "dic");
		List<Dictionary> dataList= new ArrayList<Dictionary>();
		for (Dictionary dic : list) {
			String dicttype = dic.getStr("dict_type");
			if (dicType.equals(dicttype)) {
				dataList.add(dic);
			}
		}
		return dataList;
	}
	
	/*******
	 * 查找内存中值对应的code
	 * 10.10
	 * @param dicType
	 * @param dicName
	 * @return
	 */
	public String getDicByText(String dicCode,String dict_type) {
		String str = "";
		List<Dictionary> list =CacheKit.get("dictCache", "dic");
		for (Dictionary dic : list) {
			String diccode = dic.getStr("dict_code");
			String dicname = dic.getStr("dict_name");
			String dictype = dic.getStr("dict_type");
			if (dicCode.equals(diccode) && dictype.equals(dict_type)) {
				str = dicname;
				break;
			}
		}
		return str;
	}
	
	/**
	 * 根据数据字典的类型查找对应的字典列表
	 * @param type
	 * @return
	 */
	public List<Dictionary> getDictListByType(String type){
		List<Dictionary> dictList=new ArrayList<Dictionary>();
		List<Dictionary> list =CacheKit.get("dictCache", "dic");
		for(Dictionary dict:list){
			String dictype = dict.getStr("dict_type");
			if(type.equals(dictype)){
				dictList.add(dict);
			}
		}
		return dictList;
	}
}
